package com.test.demo.base

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.KeyEvent
import android.view.WindowManager
import androidx.viewbinding.ViewBinding
import com.test.demo.R
import com.test.demo.view.BindingReflex


/**
 * @desc 弹框继承Dialog
 */
open abstract class BaseDialog<VB : ViewBinding> : Dialog {
    private var _binding: VB? = null
    protected val binding get() = _binding!!
    private var mContext: Context? = null

    constructor(context: Context) : super(context, R.style.dialog_center) {
        intView(context)
    }

    constructor(context: Context, themeStyle: Int) : super(context, themeStyle) {
        intView(context)
    }


    private fun intView(context: Context) {
        mContext = context
        _binding = BindingReflex.reflexViewBinding(javaClass, layoutInflater)!!
        this.window!!.setWindowAnimations(R.style.main_menu_animstyle)
        setContentView(_binding!!.root)

        window!!.setGravity(Gravity.BOTTOM)
        val m = window!!.windowManager
        val d = m.defaultDisplay
        val p = window!!.attributes
        p.width = WindowManager.LayoutParams.MATCH_PARENT
        p.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = p
        setCancelable(true)
        window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)

        setOnKeyListener { _, keyCode, keyEvent ->
            if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.repeatCount == 0) dismiss()
            false
        }
//        initData()
        initListener()
    }

    /**
     * 设置数据
     */
//    abstract fun initData()

    /**
     * 动作监听
     */
    abstract fun initListener()

}